﻿using _2._12._2015_Smartphone.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _2._12._2015_Smartphone.Controllers
{
    public class HomeController : Controller
    {
        private List<SmartPhone> _smartphones;

        public HomeController()
        {
            _smartphones = GetSmartPhones();
        }

        public ActionResult Index()
        {
            IndexViewModel model = new IndexViewModel();
            model.Name = "Nick's";
            model.SmartPhones = _smartphones;
            
            return View(model);
        }

        public ActionResult iPhones()
        {
            iPhoneViewModel model = new iPhoneViewModel();
            model.SmartPhones = _smartphones.OfType<iPhone>().ToList();

            return View(model);
        }

        public ActionResult Androids()
        {
            AndroidViewModel model = new AndroidViewModel();
            model.SmartPhones = _smartphones.Where(s => s.Assistant == AssistantType.GoogleNow).Cast<Android>().ToList();

            return View(model);
        }

        private static List<SmartPhone> GetSmartPhones()
        {
            List<SmartPhone> smartphones = new List<SmartPhone>();

            WindowsPhone phone1 = new WindowsPhone();
            phone1.HasGPS = true;
            phone1.MakeCall = true;
            phone1.NumberOfTiles = 4;

            iPhone phone2 = new iPhone();
            phone2.HasGPS = false;
            phone2.MakeCall = true;
            phone2.SaphireVendor = "Neat Vendor";

            Android phone3 = new Android();
            phone3.HasGPS = true;
            phone3.MakeCall = false;
            phone3.FavoriteWidget = "Weather";

            smartphones.Add(phone1);
            smartphones.Add(phone2);
            smartphones.Add(phone3);
            return smartphones;
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpGet]
        public ActionResult Create()
        {
            CreateViewModel model = new CreateViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CreateViewModel model)
        {
            _smartphones.Add(new WindowsPhone
                {
                    HasGPS = model.HasGPS,
                    MakeCall = model.MakeCall,
                    NumberOfTiles = model.NumberOfTiles
                });

            return RedirectToAction("Index");
        }
    }
}